package com.lcp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lcp.entity.ConsultationRecord;
import com.lcp.mapper.ConsultationRecordMapper;
import com.lcp.service.ConsultationRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class ConsultationRecordServiceImpl implements ConsultationRecordService {

    @Autowired
    private ConsultationRecordMapper consultationRecordMapper;

    @Override
    public ConsultationRecord getById(Long id) {
        return consultationRecordMapper.selectById(id);
    }

    @Override
    public List<ConsultationRecord> listByUserId(Long userId) {
        return consultationRecordMapper.selectList(
            new QueryWrapper<ConsultationRecord>()
                .eq("user_id", userId)
                .orderByDesc("consultation_time")
        );
    }

    @Override
    public ConsultationRecord create(ConsultationRecord record) {
        record.setCreateTime(LocalDateTime.now());
        record.setUpdateTime(LocalDateTime.now());
        consultationRecordMapper.insert(record);
        return record;
    }

    @Override
    public ConsultationRecord update(ConsultationRecord record) {
        record.setUpdateTime(LocalDateTime.now());
        consultationRecordMapper.updateById(record);
        return record;
    }

    @Override
    public void delete(Long id) {
        consultationRecordMapper.deleteById(id);
    }

    @Override
    public List<ConsultationRecord> listByDateRange(Long userId, String startDate, String endDate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime start = LocalDateTime.parse(startDate + " 00:00:00", formatter);
        LocalDateTime end = LocalDateTime.parse(endDate + " 23:59:59", formatter);
        
        return consultationRecordMapper.selectList(
            new QueryWrapper<ConsultationRecord>()
                .eq("user_id", userId)
                .between("consultation_time", start, end)
                .orderByDesc("consultation_time")
        );
    }
} 